#include <Windows.h>
#include <stdio.h>
#include <stdint.h>

/*
+-------------+
| EBML Header |
+---------------------------+
| Segment     | SeekHead    |
|             |-------------|
|             | Info        |
|             |-------------|
|             | Tracks      |
|             |-------------|
|             | Chapters    |
|             |-------------|
|             | Cluster     |
|             |-------------|
|             | Cues        |
|             |-------------|
|             | Attachments |
|             |-------------|
|             | Tags        |
+---------------------------+

df a3 8b 42 82 88 6d 61 74 72 6f 73 6b 61 18 53
80 67 08 3b ff 9f 01 11 4d 9b 74 b1 4d bb 8c 53
ab 84 15 49 a9 66 53 ac 82 ff ff 4d bb 8e 53 ab
84 11 4d 9b 74 53 ac 84 ff ff ff ff 4d bb 8e 53
ab 84 10 43 a7 70 53 ac 84 ff ff ff ff ec 82 41
15 49 a9 66 93 73 a4 90 5a cc 5f 52 30 43 53 eb
b2 74 2c 87 96 be 43 dd 10 43 a7 70 98 45 b9 95
b6 93 6e 67 90 56 14 22 f4 91 99 71 22 82 e8 bb
71 a6 20 f1 7f 19 41 a4 69 23 5d 54 61 a7 41 b5
46 6e 88 d6 b4 61 b7 be 74 91 aa 46 60 98 61 70
70 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d
73 74 72 65 61 6d 46 ae 88 d2 4c 31 69 82 2a 28
8c 46 5c 41 80 41 41


*/

int32_t EbmlVarSizeInt(int32_t nNumber, int32_t *pnSize) {
    int32_t nValue = 0;

    for (int32_t nSize = 0; nSize < 9; nSize++) {
        int32_t nBits = nSize * 7;

        if (nNumber <= (1 << nBits) - 2) {
            nValue = ((1 << nBits) + nNumber);
            *pnSize = nSize;
            //printf("size - %d - value: 0x%02x\r\n", nSize, nValue);
            return nValue;
        }
    }

    return -1;
}
int32_t wmain(int32_t nArgc, const wchar_t* pArgv[]) {
	if (nArgc < 2) {
		printf("... usage: %ws [Output MKV file path]\r\n", pArgv[0]);
	}
	else {
        int32_t nSize = 0;
        int32_t nValue = EbmlVarSizeInt(8, &nSize);
        printf("size: %d - value: 0x%02x\r\n", nSize, nValue);
	}

	return 0;
}